package me.zingon.service;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import me.zingon.common.Constant;
import me.zingon.tools.StringTool;

@Service
@Scope("session")
public class NetService {

	@Autowired
	CloseableHttpClient httpClient;

	@Autowired
	RequestConfig requestConfig;
	
	@Autowired
	StringTool stringTool;

	public CloseableHttpResponse myGet(String url, Map<String, String> params)
			throws URISyntaxException, IOException {
		URIBuilder builder = new URIBuilder(url);
		if (params!=null && !params.isEmpty()) {
			Set<java.util.Map.Entry<String, String>> set = params.entrySet();
			for (java.util.Map.Entry<String, String> entry : set) {
				builder.addParameter(entry.getKey(), entry.getValue());
			}
		} 
		HttpGet get = new HttpGet(builder.build().toString());
		get.setConfig(requestConfig);
		CloseableHttpResponse chr = httpClient.execute(get);
		return chr;
	}

	public void myGetYzm(HttpServletResponse res) throws URISyntaxException, IOException {
		HttpGet get = new HttpGet(Constant.YzmUrl + "?a=" + Math.random());
		get.setConfig(requestConfig);
		CloseableHttpResponse chr = httpClient.execute(get);
		HttpEntity he = chr.getEntity();
		BufferedImage bi = ImageIO.read(he.getContent());
		OutputStream out = res.getOutputStream();
		res.setHeader("Cache-Control", "no-cache");
		res.setHeader("Pragma", "no-cache");
		res.setDateHeader("Expires", 0);
		res.setContentType("image/gif");
		ImageIO.write(bi, "gif", out);
		out.close();
	}

	public String testUser(String xh, String sfzh, String pwd, String yzm) throws IOException {
		HttpPost post = new HttpPost(Constant.LoginUrl);
		List<NameValuePair> list = new ArrayList<NameValuePair>();
		list.add(new BasicNameValuePair("__VIEWSTATE", "/wEPDwUJODExMDE5NzY5ZGRgtUdRucUbXsT8g55XmVsTwV6PMw=="));
		list.add(new BasicNameValuePair("__VIEWSTATEGENERATOR", "6C0FF253"));
		list.add(new BasicNameValuePair("xh", xh));
		list.add(new BasicNameValuePair("sfzh", sfzh));
		list.add(new BasicNameValuePair("kl", pwd));
		list.add(new BasicNameValuePair("yzm", yzm));
		UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, "utf-8");
		post.setEntity(entity);
		post.setConfig(requestConfig);
		CloseableHttpResponse res = httpClient.execute(post);
		return EntityUtils.toString(res.getEntity(), "UTF-8");
	}

	public String getScore(String startPage, String pageSize) throws IOException {
		HttpPost httpPost = new HttpPost(Constant.ScoreUrl);
		List<NameValuePair> list = new ArrayList<NameValuePair>();
		list.add(new BasicNameValuePair("start", startPage));
		list.add(new BasicNameValuePair("pageSize", pageSize));
		UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, "utf-8");
		httpPost.setEntity(entity);
		httpPost.setConfig(requestConfig);
		CloseableHttpResponse res = httpClient.execute(httpPost);
		return EntityUtils.toString(res.getEntity(), "UTF-8");
	}
	
	
	/**
	 * 获得个人信息.
	 * @return list:0-姓名;1-学号;2-班级;3-学院
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public List<String> getBaseinfo() throws IOException {
		
		HttpGet get = new HttpGet(Constant.BaseInfoUrl); 
		get.setConfig(requestConfig);
		CloseableHttpResponse res = httpClient.execute(get);
		return stringTool.getBaseInfo(EntityUtils.toString(res.getEntity(), "UTF-8").trim());
	}
	
	public String getPjInfo() throws IOException{
		HttpGet get = new HttpGet(Constant.PjInfoUrl); 
		get.setConfig(requestConfig);
		CloseableHttpResponse res = httpClient.execute(get);
		return EntityUtils.toString(res.getEntity(),"UTF-8");
	}

	public void pj(String[] ids,String choose) throws IOException {
		for (String id : ids) {
            HttpPost post = new HttpPost(Constant.TiJiaoUrl);
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("cjid", id));
            params.add(new BasicNameValuePair("cpjg", choose));
            params.add(new BasicNameValuePair("advice", ""));
            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "utf-8");
            post.setEntity(entity);
            post.setConfig(requestConfig);
            CloseableHttpResponse res = httpClient.execute(post); 
            System.out.println(EntityUtils.toString(res.getEntity(),"UTF-8"));
        }
	}


}
